<template>
  <div class="recommend">
    <div class="recommend-product" v-if="goodsList" ref="recommend">
      <scroll class="recommend-scroll" :data="goodsList" :bounce="true" :pullup="true" @scrollToEnd="loadMore" :probeType="3" ref="scroll" :listenScroll="true" @scroll="scroll">
        <product-list :hasMore="hasMore" :goodsList="goodsList" :data="goodsList"></product-list>
      </scroll>
    </div>
    <go-top @toTop="toTop" :scrollY="scrollY"></go-top>
  </div>
</template>

<script>
import ProductList from "../../components/ProductList/productList";
import Scroll from "../../common/scroll";
import { recproductList } from "../../api/getDatas";
import goTop from "../../components/goTop/goTop";
export default {
  components: {
    Scroll,
    ProductList,
    goTop
  },
  data() {
    return {
      goodsList: null,
      count: 20,
      offset: 0,
      hasMore: false,
      scrollY: null
    };
  },
  computed: {},
  methods: {
    initData() {
      recproductList(this.count, this.offset).then(res => {
        this.goodsList = res.data.data;
        this.hasMore = res.data.has_more;
      });
    },

    loadMore() {
      let offset = (this.offset += this.count);
      recproductList(this.count, offset).then(res => {
        this.goodsList = this.goodsList.concat(res.data.data);
        this.hasMore = res.data.has_more;
      });
    },
    toTop() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    scroll(pos) {
      this.scrollY = -pos.y;
    }
  },
  created() {
    this.initData();
  }
};
</script>

<style lang="less" scoped>
.recommend-product {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 50px;
  background-color: #f1f2f6;
  .recommend-scroll {
    height: 100%;
    overflow: hidden;
  }
}
</style>

